<template>

    <Function
            :list="options.list"
            :create="options.create"
            :fields="options.fields"
            :form-options="options.formOptions"
            :operateOptions="options.operateOptions"
    ></Function>
</template>
<script setup>
import Function from "@/components/core/function/index.vue";
import {markRaw, reactive} from "vue";
import {
    createModelColumn,
    pageModelColumn,
    validateModelColumnName,
    validateModelColumnDisplayName
} from "@/api/api.model_column";
import {app, company} from "@/utils/core/store";
import {ElMessage, ElMessageBox} from "element-plus";
import SelectColumnType from '@/views/comps/dictionary/columnType.vue'

const dialog = reactive({
    visible: false,
    title: '创建数据源',
    id: '35901782295052288',
    active: 2
})

const props = defineProps({
    model: {},
    dbmsId: {}
})


const options = reactive({
    list: {
        api: pageModelColumn,
        fixParams: {companyId: company.value.id, appId: app.value.id},
        sort: 'updateAt_desc;createAt_desc'
    },
    create: {
        api: createModelColumn,
        title: '新增字段'
    },
    operateOptions: {
        operate: {
            display: true,
            width: '200px',
            buttons: [{
                label: '模型设计',
                display: true,
                doClick: (row) => {
                    domainDesignRef.value.openDialog(row);
                }
            }, {
                label: '数据管理',
                display: true,
                doClick: (row) => {

                }
            }, {
                label: '删除',
                display: true,
                doClick: (row) => {

                }
            }]
        }
    },
    formOptions: {
        width: 'auto',
        onBeforeSubmit: (forms) => {
            forms.companyId = company.value.id;
            forms.appId = app.value.id;
            forms.modelId = props.model.id;
            forms.tableName = props.model.tableName;
            forms.dbmsId = props.dbmsId;
            ;
        },
        onAfterSubmit: (data) => {
            // ElMessageBox.confirm('请确定\n' + data.script, '请确定', {
            //     confirmButtonText: '确认'
            // }).then(() => {
            //     // 执行数据库脚本
            //     executeModel({id: data.id}).then(res => {
            //         if (res.data) {
            //             ElMessage.success("更新成功");
            //         } else {
            //             ElMessage.error("更新失败");
            //         }
            //     }).catch((e) => {
            //         ElMessage.error("新增失败:" + e);
            //     })
            // });
        }
    },
    fields: [
        {
            name: 'name',
            label: '字段名称',
            type: 'input',
            required: true,
            showForm: true,
            showList: true,
            validate: validateModelColumnName,
            placeholder: '请输入字段名称'
        },
        {
            name: 'displayName',
            label: '显示名称',
            type: 'input',
            required: true,
            showForm: true,
            showList: true,
            validate: validateModelColumnDisplayName,
            placeholder: '请输入显示名称'
        },
        {
            name: 'type',
            label: '字段类型',
            required: true,
            showForm: true,
            showList: true,
            component: markRaw(SelectColumnType),
            validate: validateModelColumnName,
            placeholder: '请输入字段名称'
        },
        {
            name: 'defaultValue',
            label: '默认值',
            type: 'input',
            showForm: true,
            showList: true,
            placeholder: '请输入字段默认值'
        },
        {
            name: 'len',
            label: '字段长度',
            type: 'input',
            showForm: true,
            showList: true,
            placeholder: '请输入字段长度'
        },
        {
            name: 'nullable',
            label: '允许空值',
            type: 'switch',
            showForm: true,
            showList: true
        },
        {
            name: 'isUnique',
            label: '是否唯一',
            type: 'switch',
            showForm: true,
        },
        {
            name: 'isSearch',
            label: '是否可搜',
            type: 'switch',
            showForm: true,
        },
        {
            name: 'remark',
            label: '注释',
            type: 'textarea',
            showForm: true,
        },
        {
            name: 'createAt',
            label: '更新时间',
            showList: true
        },
        {
            name: 'updateAt',
            label: '更新时间',
            showList: true
        }
    ]
})
</script>
